package com.qu.waibao.search;

import android.graphics.Color;
import android.icu.util.ValueIterator;
import android.opengl.ETC1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.othershe.nicedialog.BaseNiceDialog;
import com.othershe.nicedialog.NiceDialog;
import com.othershe.nicedialog.ViewConvertListener;
import com.othershe.nicedialog.ViewHolder;
import com.qu.waibao.AppConfig;
import com.qu.waibao.R;
import com.qu.waibao.base.BaseActivity;
import com.qu.waibao.base.BaseRecyclerAdapter;
import com.qu.waibao.bean.Shop;
import com.qu.waibao.home.adpater.ShopAdapter;
import com.qu.waibao.interf.XnetCallBack;
import com.qu.waibao.network.ActionUtil;
import com.qu.waibao.network.NetManager;
import com.qu.waibao.utils.GsonUtil;
import com.qu.waibao.widget.RecyclerRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class ShopSearchActivity extends BaseActivity implements ShopAdapter.OnItemBuyClickListener,
        RecyclerRefreshLayout.SuperRefreshLayoutListener,View.OnClickListener{
    @BindView(R.id.mRecyclerRefreshLayout)
    RecyclerRefreshLayout mRecyclerRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.et_search)
    EditText et_search;
    private ShopAdapter adapter;
    private boolean isRefreshing = true;
    private int pageNum = 1;
    private String keywords = "";
    @Override
    protected int getContentView() {
        return R.layout.activity_shop_search;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mRecyclerRefreshLayout.setColorSchemeResources(R.color.colorPrimaryDark);
        mRecyclerRefreshLayout.setSuperRefreshLayoutListener(this);
        et_search.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(keyCode==KeyEvent.KEYCODE_ENTER && event.getAction()==KeyEvent.ACTION_UP){
                    keywords = et_search.getText().toString().trim();
                    if(TextUtils.isEmpty(keywords)){
                        keywords = "";
                    }
                    isRefreshing = true;
                    requestData();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        adapter = new ShopAdapter(this, BaseRecyclerAdapter.ONLY_FOOTER);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        if(adapter.getItems().size() == 0){
            mRecyclerRefreshLayout.setRefreshing(true);
            onRefreshing();
        }
        adapter.setOnItemBuyClickListener(this);
    }

    @Override
    public void onItemBuyClick(int position) {
        final String price = adapter.getItems().get(position).getPrice();
        final String jifen  = adapter.getItems().get(position).getJf();
        NiceDialog.init()
                .setLayoutId(R.layout.shop_dailog_layout)     //设置dialog布局文件
                .setConvertListener(new ViewConvertListener() {     //进行相关View操作的回调
                    @Override
                    public void convertView(final ViewHolder holder, final BaseNiceDialog dialog) {
                        TextView textView = holder.getView(R.id.tv_title);
                        textView.setText("支付"+price+"元或"+jifen+"积分");
                        final TextView tv_weixin = holder.getView(R.id.tv_weixin);
                        final TextView tv_zhifubao = holder.getView(R.id.tv_zhifubao);
                        final TextView tv_jifen = holder.getView(R.id.tv_jifen);
                        holder.setOnClickListener(R.id.iv_close, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                        holder.setOnClickListener(R.id.ll_weixin, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_weixin.setTextColor(Color.parseColor("#67b8f2"));
                                tv_zhifubao.setTextColor(Color.parseColor("#333333"));
                                tv_jifen.setTextColor(Color.parseColor("#333333"));
                            }
                        });
                        holder.setOnClickListener(R.id.ll_zhifubao, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_weixin.setTextColor(Color.parseColor("#333333"));
                                tv_zhifubao.setTextColor(Color.parseColor("#67b8f2"));
                                tv_jifen.setTextColor(Color.parseColor("#333333"));
                            }
                        });
                        holder.setOnClickListener(R.id.ll_jifen, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_weixin.setTextColor(Color.parseColor("#333333"));
                                tv_zhifubao.setTextColor(Color.parseColor("#333333"));
                                tv_jifen.setTextColor(Color.parseColor("#67b8f2"));
                            }
                        });
                        holder.setOnClickListener(R.id.tv_sure, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                    }
                })
                .setDimAmount(0.3f)     //调节灰色背景透明度[0-1]，默认0.5f
                .setShowBottom(true)     //是否在底部显示dialog，默认flase
                .setOutCancel(true)     //点击dialog外是否可取消，默认true
                .show(getSupportFragmentManager());     //显示dialog
    }

    public void requestData(){
        Map<String,String> map = new HashMap<>();
        map.put("action", ActionUtil.quanshop_list_get);
        map.put("userid", AppConfig.getString("userid"));
        map.put("keyword",keywords);
        map.put("pagenum",pageNum+"");
        NetManager.getInstance().request(this, map, false, new XnetCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                List<Shop> list = GsonUtil.createGson().fromJson(result,
                        new TypeToken<List<Shop>>(){}.getType());
                if(isRefreshing){
                    adapter.clear();
                    adapter.addAll(list);
                    if(list.size() < 12){
                        adapter.setState(BaseRecyclerAdapter.STATE_NO_MORE,true);
                    }
                }else {
                    adapter.addAll(list);
                    if(list.size() < 12){
                        adapter.setState(BaseRecyclerAdapter.STATE_NO_MORE,true);
                    }
                    if(list.size() == 0){
                        pageNum--;
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if(pageNum > 1){
                    pageNum--;
                    onCompalte();
                }
            }

            @Override
            public void onFinished() {
                onCompalte();
            }
        });
    }
    public void onCompalte(){
        mRecyclerRefreshLayout.onComplete();
        isRefreshing = false;
    }

    @Override
    public void onRefreshing() {
        isRefreshing = true;
        pageNum = 1;
        requestData();
    }

    @Override
    public void onLoadMore() {
        isRefreshing = false;
        pageNum++;
        adapter.setState(BaseRecyclerAdapter.STATE_LOAD_MORE,true);
        requestData();
    }
    @OnClick({R.id.ll_back,R.id.tv_search})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_search:
                keywords = et_search.getText().toString().trim();
                if(TextUtils.isEmpty(keywords)){
                    keywords = "";
                }
                isRefreshing = true;
                requestData();
                break;
        }
    }
}
